//
//  WBStatusListViewModel.swift
//  Weibo
//
//  Created by wade on 17/2/22.
//  Copyright © 2017年 wade. All rights reserved.
//

import Foundation

class WBStatusListViewModel {
    
    lazy var statusList = [WBStatus]()
    
    var isMorePullup = true
    
    func loadStatus(pullup:Bool, completion:@escaping (Bool,Bool)->()) {
        
        if pullup && !isMorePullup
        {
            completion(false,false)
        }
        
        let sinceid = pullup ? 0 : (statusList.first?.id ?? 0)
        
        let maxid = !pullup ? 0 : (statusList.last?.id ?? 0)
        
        WBNetworkManager.shared.statusList(since_id: sinceid ,max_id: maxid,completion: { (list, bool) in
           
            guard let array = NSArray.yy_modelArray(with: WBStatus.self, json: list ?? []) as? [WBStatus] else{
            
                completion(bool,self.isMorePullup)
                
                return
            
            }
            
            print("刷新到\(array.count)")
            
            self.isMorePullup = (array.count == 0) ? false : true
          
            if pullup{
                
                self.statusList += array
                
            }
            else
            {
                self.statusList = array + self.statusList
            }
            
            
            completion(bool,self.isMorePullup)
            
            
        })
        
    }
    
}
